#include <REGX52.h>
#include "I2C.h"
//AT24C02的从机地址写，从机地址读需 |= 0x01
#define AT24C02_ADDRESS 0xA0

/**
  * @brief		向AT24C02暂存器写入数据
  * @parm		WordAddress:写到暂存器的什么地址
  * @parm		Data:要写入的数据	
  * @retval		无
  */
void AT24C02_WriteByte(unsigned char WordAddress, unsigned char Data)
{
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);
	I2C_ReceiveAck();
	I2C_SendByte(WordAddress);
	I2C_ReceiveAck();
	I2C_SendByte(Data);
	I2C_ReceiveAck();
	I2C_Stop();
}
/**
  * @brief		从AT24C02读取数据
  * @parm		WordAddress:读什么地址的数据
  * @retval		读出的数据
  */
unsigned char AT24C02_ReadByte(unsigned char WordAddress)
{
	unsigned char Data;
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);
	I2C_ReceiveAck();
	I2C_SendByte(WordAddress);
	I2C_ReceiveAck();
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS | 0x01);//此处是读
	I2C_ReceiveAck();
	Data = I2C_ReceiveByte();
	I2C_SendAck(1);
	I2C_Stop();
	return Data;
}